#include "lxz_config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>

#include "lxz_types.h"
#include "os_port.h"
#include "lxz_runlog.h"
#include "lxz_debug.h"
#include "lxz_ring.h"
#include "lxz_string.h"
#include "lxz_dstring.h"
#include "lxz_atc_req.h"
#include "lxz_urc_table.h"

#include "lxz_dbg_vmem.h"

static FILE* gfp_log_file = NULL;

sint32 lxz_runlog_f_init(uint08* u_rel_dir)
{
    int i_ret_val = 0;

    char llbuf[63];
    int i_str_len = 0;
    FILE* fp_pre_file = NULL;
    FILE* fp_log_file = NULL;
    sint08* p_tmp_dir = NULL;
    sint32 i_dir_exist = 0;
    
    time_t atime;
    int i_cur_year;
    int i_cur_mon;
    int i_cur_day;
    int i_cur_hour;
    int i_cur_min;
    int i_cur_sec;
    struct tm* p = NULL;

    time(&atime);
    p = gmtime(&atime);

    memset(llbuf, 0, sizeof(llbuf));
    i_cur_year = p->tm_year+1900;
    i_cur_mon = p->tm_mon+1;
    i_cur_day = p->tm_mday;
    i_cur_hour = p->tm_hour+8;
    i_cur_min = p->tm_min;
    i_cur_sec = p->tm_sec;

#if 0
	sprintf(llbuf, "./lxzat%d%02d%02d%02d%02d%02d.log", i_cur_year, 
        i_cur_mon, i_cur_day, i_cur_hour, i_cur_min, i_cur_sec);
#else
    sprintf(llbuf, "./lxzat%d%02d%02d.log", i_cur_year, i_cur_mon, i_cur_day);
#endif

    fp_log_file = fopen(llbuf, "wb+");
    if (fp_log_file == NULL)
    {
        return i_ret_val;
    }

    i_ret_val = 1;
    fp_pre_file = gfp_log_file;
    gfp_log_file = fp_log_file;
    fflush(fp_log_file);

    if (fp_pre_file != NULL)
    {
        memset(llbuf, 0, sizeof(llbuf));
#if 0
        i_str_len = sprintf(llbuf, "%slxzat%d%02d%02d%02d%02d%02d.log", 
			"Next-logfile: ",i_cur_year, i_cur_mon, 
			i_cur_day, i_cur_hour, i_cur_min, i_cur_sec);
#else
        i_str_len = sprintf(llbuf, "%slxzat%d%02d%02d%02d%02d%02d.log", 
			"Next-logfile: ",i_cur_year, i_cur_mon, i_cur_day);
#endif
        fwrite(llbuf, 1, i_str_len, fp_pre_file);
        fflush(fp_pre_file);
        fclose(fp_pre_file);
    }

    return i_ret_val;
}

sint32 lxz_runlog_f_printf(const char * vfmt, ...)
{
    char llbuf[512];

    va_list ap;
    int i_nb_data = 0;
	int i_nb_free = 0;

    time_t atime;
    int i_cur_year;
    int i_cur_mon;
    int i_cur_day;
    int i_cur_hour;
    int i_cur_min;
    int i_cur_sec;
    struct tm* p = NULL;
    FILE* fp_log_file = gfp_log_file;

    if (fp_log_file != NULL)
    {
        time(&atime);
        p = gmtime(&atime);
        
        i_cur_year = p->tm_year+1900;
        i_cur_mon = p->tm_mon+1;
        i_cur_day = p->tm_mday;
        i_cur_hour = p->tm_hour+8;
        i_cur_min = p->tm_min;
        i_cur_sec = p->tm_sec;

        i_nb_free = sizeof(llbuf);
        memset(llbuf, 0, i_nb_free);
        i_nb_data = sprintf(llbuf, "[%04d-%02d-%02d %02d:%02d:%02d]", 
            i_cur_year, i_cur_mon, i_cur_day, 
            i_cur_hour, i_cur_min, i_cur_sec);

        i_nb_free = i_nb_free - i_nb_data;
        va_start(ap,vfmt);
        i_nb_data = vsnprintf(&(llbuf[i_nb_data]), i_nb_free, vfmt, ap);
        va_end(ap);

        i_nb_data = strlen(llbuf);
        fwrite(llbuf, 1, i_nb_data, fp_log_file);
        fflush(fp_log_file);
    }
    
    return i_nb_data;
}

sint32 lxz_runlog_f_fsize(void)
{
    sint32 i_file_size = 0;
    FILE* fp_log_file = gfp_log_file;

    if (fp_log_file != NULL)
    {
        i_file_size = ftell(fp_log_file);
    }
    
    return i_file_size;
}

#define LXZ_DBG_OCTET_NUM 16
sint32 lxz_runlog_f_printfhex(const char * vbuf, sint32 ilen)
{
    sint32 i = 0;
    char * pbuf = (char *)vbuf;
    char lhexbuf[4 * LXZ_DBG_OCTET_NUM];
    
    lxz_runlog_f_printf("\r\n");
    lxz_runlog_f_printf("===HEX String Begin===\r\n");
    while (ilen >= LXZ_DBG_OCTET_NUM)
    {
        memset(lhexbuf, 0, sizeof(lhexbuf));
        
        i = 0;
        while (i < LXZ_DBG_OCTET_NUM)
        {
            sprintf(&(lhexbuf[3 * i]), "%02X ", pbuf[i]);
            i++;
        }

        ilen = ilen - LXZ_DBG_OCTET_NUM;
        pbuf = pbuf + LXZ_DBG_OCTET_NUM;
        
        lxz_runlog_f_printf("HEX: %s\r\n", lhexbuf);
    }
    
    if (ilen>0)
    {
        memset(lhexbuf, 0, sizeof(lhexbuf));

        i = 0;
        while (i < ilen)
        {
            sprintf(&(lhexbuf[3 * i]), "%02X ", pbuf[i]);
            i++;
        }

        pbuf = pbuf + ilen;
        ilen = 0;
        
        lxz_runlog_f_printf("HEX: %s\r\n", lhexbuf);
    }

    lxz_runlog_f_printf("===HEX String End===\r\n");
    lxz_runlog_f_printf("\r\n");
    
    return (pbuf - vbuf);
}

sint32 lxz_runlog_f_reset(void)
{
    FILE* fp_log_file = gfp_log_file;

    if (fp_log_file != NULL)
    {
        lxz_runlog_f_printf("runlog exit\r\n");

        fclose(fp_log_file);
        gfp_log_file = fp_log_file = NULL;
    }

    return 1;
}


